﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyDMS.DAL;
using ExtAspNet;
using TreeNode = ExtAspNet.TreeNode;
using Department = MyDMS.DAL.Department;

namespace MyDMS_ExtJS
{
    public partial class Main : PageBase
    {
        MyDMSEntity _myDmsEntity = new MyDMSEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            BindTree();
        }

        private void BindTree()
        {
            if (!IsPostBack)
            {
                #region 档案菜单

                if (Author != null)
                {
                    if (!Author.is_Administrator)
                    {
                        foreach (var dept in _myDmsEntity.Department)
                        {
                            bool _iscontained2 = false;

                            TreeNode deptTn = new TreeNode();
                            deptTn.Text = dept.Department_Name;
                            deptTn.NavigateUrl = string.Format("./Department.aspx?dept={0}", dept.Department_Code);

                            if (Author.department_Code == dept.Department_Code)
                            {
                                _iscontained2 = true;
                            }
                            foreach (
                                var proj in _myDmsEntity.Project.Where(p => p.Department_Code == dept.Department_Code))
                            {
                                #region 控制是否显示 意义不大

                                bool _iscontained1 = false;
                                if (proj.Is_Anonymous)
                                {
                                    _iscontained1 = true;
                                    _iscontained2 = true;
                                }
                                if (Author.department_Code == dept.Department_Code)
                                {
                                    _iscontained1 = true;
                                    _iscontained2 = true;
                                }
                                if ((!proj.Is_Anonymous) && (Author.department_Code != dept.Department_Code))
                                {
                                    _iscontained1 = false;
                                }

                                #endregion

                                if (_iscontained1)
                                {
                                    TreeNode projTn = new TreeNode();
                                    projTn.Text = proj.Project_Title;
                                    projTn.NavigateUrl = string.Format("./Project.aspx?dept={0}&proj={1}",
                                                                       dept.Department_Code,
                                                                       proj.Project_Code);
                                    foreach (
                                        var task in _myDmsEntity.Task.Where(t => t.Project_Code == proj.Project_Code))
                                    {
                                        TreeNode taskTn = new TreeNode();
                                        taskTn.Text = task.Task_Title;
                                        taskTn.NavigateUrl = string.Format("./Task.aspx?dept={0}&proj={1}&task={2}",
                                                                           dept.Department_Code,
                                                                           proj.Project_Code,
                                                                           task.Task_Code);
                                        foreach (
                                            var folder in
                                                _myDmsEntity.Folder.Where(df => df.Task_Code == task.Task_Code))
                                        {
                                            TreeNode folderTn = new TreeNode();
                                            folderTn.Text = folder.Folder_Title;
                                            folderTn.NavigateUrl =
                                                string.Format("./Folder.aspx?dept={0}&proj={1}&task={2}&folder={3}",
                                                              dept.Department_Code, proj.Project_Code, task.Task_Code,
                                                              folder.Folder_Code);
                                            taskTn.Nodes.Add(folderTn);
                                        }
                                        projTn.Nodes.Add(taskTn);
                                    }
                                    deptTn.Nodes.Add(projTn);
                                }
                            }
                            if (_iscontained2)
                            {
                                treeMenu.Nodes.Add(deptTn);
                            }
                        }
                    }
                }

                #endregion

                #region 管理菜单
                if (Author != null)
                {
                    if (Author.is_Administrator)
                    {
                        treeMenu.Nodes.Add(new TreeNode()
                                                {
                                                    Text = "用户管理",
                                                    NavigateUrl = "./Admin/User.aspx"
                                                });
                        treeMenu.Nodes.Add(new TreeNode()
                                                {
                                                    Text = "科室管理",
                                                    NavigateUrl = "./Admin/Department.aspx"
                                                });
                        treeMenu.Nodes.Add(new TreeNode()
                                                {
                                                    Text = "中项管理",
                                                    NavigateUrl = "./Admin/Project.aspx"
                                                });
                        treeMenu.Nodes.Add(new TreeNode()
                                                {
                                                    Text = "小项管理",
                                                    NavigateUrl = "./Admin/Task.aspx"
                                                });
                        treeMenu.Nodes.Add(new TreeNode()
                                                {
                                                    Text = "分项管理",
                                                    NavigateUrl = "./Admin/Folder.aspx"
                                                });

                        treeMenu.Nodes.Add(new TreeNode()
                                                {
                                                    Text = "备份管理",
                                                    NavigateUrl = "./Admin/Backup.aspx"
                                                });

                        treeMenu.Nodes.Add(new TreeNode()
                                                {
                                                    Text = "档案类型",
                                                    NavigateUrl = "./Admin/DocumentType.aspx"
                                                });
                        treeMenu.Nodes.Add(new TreeNode()
                                                {
                                                    Text = "密级管理",
                                                    NavigateUrl = "./Admin/SecretLevel.aspx"
                                                });
                        treeMenu.Nodes.Add(new TreeNode()
                        {
                            Text = "保存期限管理",
                            NavigateUrl = "./Admin/ShelfLife.aspx"
                        });
                        treeMenu.Nodes.Add(new TreeNode()
                        {
                            Text = "用户角色",
                            NavigateUrl = "./Admin/UserRole.aspx"
                        });
                        Tab1.IFrameUrl = "./Admin/Help.aspx";
                    }
                }

                #endregion
            }
        }

        protected void btnExit_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            FormsAuthentication.RedirectToLoginPage();
        }


        protected int GetOnlineCount()
        {
            DateTime onlineDuration = DateTime.Now.AddMinutes(-15);
            return _myDmsEntity.User.Count(u => u.Updation_Date >= onlineDuration);
        }
    }
}